﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

using XP.Utilities.Utility;

namespace XP.Utilities.Utility
{
    [Serializable]
    public class Register
    {
        private string _encryptKey;

        public string AppFolder
        {
            get
            {
                return System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) + "\\" + Application.ProductName + "\\";
            }
        }

        #region 构造函数
        public Register()
            : this("REGDATE$")
        {
        }
        /// <summary>
        /// 生成指定密钥加密的数据
        /// </summary>
        /// <param name="encryptKey">密钥</param>
        public Register(string encryptKey)
        {
            _encryptKey = encryptKey;
        }
        #endregion

        public string Date
        {
            get;
            set;
        }
        public DateTime NowDate
        {
            get;
            set;
        }
        /// <summary>
        /// 验证是否到期
        /// </summary>
        /// <param name="days">指定的到期天数</param>
        /// <returns></returns>
        public bool IsExpires(int days)
        {
            string filePath = this.AppFolder + "date.xml";
            try
            {
                if (!System.IO.File.Exists(filePath))
                {
                    if (!System.IO.Directory.Exists(this.AppFolder))
                    {
                        System.IO.Directory.CreateDirectory(this.AppFolder);
                    }
                    this.Date = Security.Security.EncryptDES(DateTime.Today.ToString("yyyy-MM-dd"), _encryptKey);
                    this.NowDate = DateTime.Today;
                    XmlSerializer.SerializeToFile<Register>(this, filePath);
                    return false;
                }
                else
                {
                    Register reg = XmlSerializer.DeserializeFromFile<Register>(filePath);
                    string decode = Security.Security.DecryptDES(reg.Date, _encryptKey);
                    DateTime regDate = DateTime.Parse(decode);
                    TimeSpan tSpan = reg.NowDate.Subtract(regDate);
                    if (tSpan.TotalDays < days)
                    {
                        reg.NowDate = DateTime.Now;
                        XmlSerializer.SerializeToFile<Register>(reg, filePath);
                        return false;
                    }
                    else
                    {
                        MessageBox.Show("注册码已过期请重新注册！ ", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("验证注册码时发生意外：{0}", ex.Message), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }
        }
    }
}
